﻿/***************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace BaoJianSoft.Web.Extensions.DynamicPages
{
	/// <summary>
	/// DataSourceLoad EventArgs
	/// </summary>
	[Serializable]
	[XmlRoot("DataSourceLoadEventArgs")]
	public sealed class DataSourceLoadEventArgs : EventArgs
	{
		/// <summary>
		/// Gets querying criterias
		/// </summary>
		[XmlArray("Criterias")]
		[XmlArrayItem("Criteria")]
		public Collection<QueryCriteria> Criterias { get; set; }

		/// <summary>
		/// Get predicate to build query criteria.
		/// </summary>
		[XmlElement("Predicate")]
		public string Predicate { get; private set; }

		/// <summary>
		/// Get predicate parameters to format query criteria.
		/// </summary>
		[XmlArray("Parameters")]
		[XmlArrayItem("Parameter")]
		public object[] Parameters { get; private set; }

		/// <summary>
		/// Get current paging index.
		/// </summary>
		[XmlElement("Predicate")]
		public int PageIndex { get; private set; }

		/// <summary>
		/// Get page size
		/// </summary>
		[XmlElement("Predicate")]
		public int PageSize { get; private set; }

		/// <summary>
		/// Get sorting expression
		/// </summary>
		[XmlElement("Predicate")]
		public string SortExpression { get; private set; }

		/// <summary>
		/// Set/get total record count.
		/// </summary>
		[XmlIgnore]
		public int RecordCount { get; set; }

		/// <summary>
		/// Set/get data source bound to gridview.
		/// </summary>
		[XmlIgnore]
		public IList DataSource { get; set; }

		/// <summary>
		/// Constructor
		/// </summary>
		public DataSourceLoadEventArgs()
		{
		}

		/// <summary>
		/// Construct DataSourceLoad EventArgs
		/// </summary>
		/// <param name="predicate"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="sortExpression"></param>
		/// <param name="predicateParameters"></param>
		public DataSourceLoadEventArgs(string predicate, int pageIndex, int pageSize, string sortExpression, params object[] predicateParameters)
			: this(null, predicate, pageIndex, pageSize, sortExpression, predicateParameters)
		{
		}

		/// <summary>
		/// Construct DataSourceLoad EventArgs
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="sortExpression"></param>
		public DataSourceLoadEventArgs(IEnumerable<QueryCriteria> criterias, int pageIndex, int pageSize, string sortExpression)
			: this(criterias, null, pageIndex, pageSize, sortExpression, null)
		{
		}

		/// <summary>
		/// Construct DataSourceLoad EventArgs
		/// </summary>
		/// <param name="predicate"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="sortExpression"></param>
		/// <param name="predicateParameters"></param>
		public DataSourceLoadEventArgs(IEnumerable<QueryCriteria> criterias, string predicate, int pageIndex, int pageSize, string sortExpression, params object[] predicateParameters)
		{
			if (criterias != null)
				this.Criterias = new Collection<QueryCriteria>(criterias.ToList());

			this.Predicate = predicate;
			this.PageIndex = pageIndex;
			this.PageSize = pageSize;
			this.SortExpression = sortExpression;
			this.Parameters = predicateParameters;
		}
	}
}
